package com.lirh.mysql.service;

import com.lirh.mysql.dao.ProductDao;
import com.lirh.mysql.dao.PurchaseRecordDao;
import com.lirh.mysql.entity.Product;
import com.lirh.mysql.entity.PurchaseRecord;
import io.lettuce.core.dynamic.Commands;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisCommands;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private PurchaseRecordDao purchaseRecordDao;
    @Autowired
      private RedisConnectionFactory redisConnectionFactory;


    private static RedisCommands COMMANDS;
    String lua="redis.call('set','lirh','hello lua!')\n" +
            "return 1";

    String purchaseScript="redis.call('sadd',KEYS[1],ARGV[2])\n" +
            "local productPurchaseList=KEYS[2]..ARGV[2]\n" +
            "local userId=ARGV[1]\n" +
            "local product='product_'..ARGV[2]\n" +
            "local quantity=tonumber(ARGV[3])\n" +
            "local stock=tonumber(redis.call('hget',product,'stock'))\n" +
            "local price=tonumber(redis.call('hget',product,'price'))\n" +
            "local purchase_date=ARGV[4]\n" +
            "if stock<quantity then return 0 end\n" +
            "stock=stock-quantity\n" +
            "redis.call('hset',product,'stock',tostring(stock))\n" +
            "local sum=quantity*price\n" +
            "local pruchaseRecord=userId..','..quantity..','..sum..','..price..','..purchase_date\n" +
            "redis.call('rpush',productPurchaseList,pruchaseRecord)\n" +
            "return 1\n";
    private static final String PURCHASE_PRODUCT_LIST="product_list_";
    private static final String PRODUCT_SCHEDULE_SET="product_schedule_set";
    private String sha1=null;
    public boolean purchase(Integer userId,Integer productId,Integer quantity){
        Long timeMillis = System.currentTimeMillis();
        try {
            RedisConnection connection = redisConnectionFactory.getConnection();
if (sha1==null){
            sha1 = connection.scriptLoad(purchaseScript.getBytes());
}
            Object res = connection.evalSha(sha1, ReturnType.INTEGER, 2, PRODUCT_SCHEDULE_SET.getBytes(), PURCHASE_PRODUCT_LIST.getBytes(), userId.toString().getBytes(), productId.toString().getBytes(), quantity.toString().getBytes(), timeMillis.toString().getBytes());
            Long res1 = (Long) res;
            return res1 ==1;

        }finally {

        }

//        Product product = productDao.selectById(productId);
//        if (product.getStock()<quantity){
//            return  false;
//        }
//        productDao.decreaseProduct(productId,quantity);
//
//        PurchaseRecord record = new PurchaseRecord();
//        record.setProductId(productId);
//        record.setUserId(userId);
//        record.setQuantity(quantity);
//        record.setSum(quantity*(product.getPrice()));
//        purchaseRecordDao.insert(record);
//        return true;
    }
}
